package cn.iocoder.yudao.module.wine.controller.app.leaveinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.wine.controller.admin.leaveinfo.vo.LeaveInfoSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.module.wine.controller.app.leaveinfo.vo.*;
import cn.iocoder.yudao.module.wine.service.leaveinfo.LeaveInfoService;

@Tag(name = "用户 APP - 离职通告")
@RestController
@RequestMapping("/wine/leave-info")
@Validated
public class AppLeaveInfoController {

    @Resource
    private LeaveInfoService leaveInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建离职通告")
    public CommonResult<Integer> createLeaveInfo(@Valid @RequestBody LeaveInfoSaveReqVO createReqVO) {
        return success(leaveInfoService.appCreateLeaveInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新发布离职通告")
    public CommonResult<Boolean> updateLeaveInfo(@Valid @RequestBody LeaveInfoSaveReqVO updateReqVO) {
        leaveInfoService.appUpdateLeaveInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "下架离职通告")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteLeaveInfo(@RequestParam("id") Integer id) {
        leaveInfoService.appDeleteLeaveInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得离职通告详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppLeaveInfoRespVO> getLeaveInfo(@RequestParam("id") Integer id) {
        AppLeaveInfoRespVO leaveInfo = leaveInfoService.appGetLeaveInfo(id);
        if (leaveInfo != null) {
            leaveInfo.setUserAreaName(AreaUtils.format(leaveInfo.getUserAreaId()," "));
        }
        return success(leaveInfo);
    }

    @GetMapping("/list")
    @Operation(summary = "获取离职通告列表")
    public CommonResult<PageResult<AppLeaveInfoRespVO>> getLeaveInfoList(@Valid AppLeaveInfoPageReqVO pageReqVO) {
        PageResult<AppLeaveInfoRespVO> page = leaveInfoService.getLeaveInfoList(pageReqVO);
        List<AppLeaveInfoRespVO> list = page.getList();
        List<AppLeaveInfoRespVO> collect = list.stream().peek(item -> {
            item.setAreaName(AreaUtils.format(item.getAreaId()," "));
            item.setUserAreaName(AreaUtils.format(item.getUserAreaId()," "));
        }).collect(Collectors.toList());
        page.setList(collect);
        return success(page);
    }

    @GetMapping("/my-publish-list")
    @Operation(summary = "我发布的离职通告列表")
    public CommonResult<List<AppLeaveInfoRespVO>> getMyPublishLeaveInfoList(@Valid AppLeaveInfoPageReqVO pageReqVO) {
        List<AppLeaveInfoRespVO> list = leaveInfoService.getMyPublishLeaveInfoList(pageReqVO);
        List<AppLeaveInfoRespVO> collect = list.stream().peek(item -> {
            item.setAreaName(AreaUtils.format(item.getAreaId()," "));
            item.setUserAreaName(AreaUtils.format(item.getUserAreaId()," "));
        }).collect(Collectors.toList());
        return success(collect);
    }
}
